package cn.liberals.system.service.impl;

import cn.liberals.system.domain.Role;
import cn.liberals.system.mapper.RoleMapper;
import cn.liberals.system.service.IRoleService;
import cn.liberals.basic.service.impl.BaseServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liberals
 * @since 2022-07-11
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    /*
    * @Title: update
    * @Description: 修改角色
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/13 12:53
    * @Parameters:
    * @Return
    */
    @Override
    public void save(Role role){
        // 保存角色信息
        roleMapper.save(role);
        // 保存主表之后同步操作,初始化关联表
        initRelationTable(role);
    }
    
    /* 
    * @Title: initRelationTable
    * @Description: 初始化关联表
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/13 11:41  
    * @Parameters: 
    * @Return 
    */
    public void initRelationTable(Role role){
        // 1.根据roleId删除角色菜单和角色权限关联表对应数据
        roleMapper.removeRoleMenu(role.getId());
        roleMapper.removeRolePermission(role.getId());
        // 根据roleId和菜单Id和权限Id进行新增
        roleMapper.saveRoleMenu(role.getId(),role.getMenus());
        roleMapper.savePermission(role.getId(),role.getPermissions());
    }


    @Override
    public void remove(Long id) {
        super.remove(id);
        roleMapper.removeRoleMenu(id);
        roleMapper.removeRolePermission(id);
    }

    @Override
    public void update(Role role) {
        // 保存角色信息
        roleMapper.update(role);
        // 保存主表之后同步操作,初始化关联表
        initRelationTable(role);
    }
}
